﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArgusLib.Fractals.OrbitFractals;

namespace Fraktalysator.Controls
{
	public partial class RendererEscapeTimeSmootherInputControl : UserControl, IRenderParameterInputControl
	{
		#region Private Fields
		bool requestRedraw = true;
		#endregion

		#region Constructor
		public RendererEscapeTimeSmootherInputControl()
		{
			InitializeComponent();
			this.cbColoring.Initialize(typeof(RenderParameterEscapeTimeSmoother.Colorings), Localization.Enums.ResourceManager);
			this.rbNumberOfPeriods.Checked = true;
		}
		#endregion

		#region Properties
		public RenderParameterEscapeTimeSmoother Parameter
		{
			get
			{
				RenderParameterEscapeTimeSmoother parameter = new RenderParameterEscapeTimeSmoother();
				parameter.AutoSetFrequency = this.rbNumberOfPeriods.Checked;
				parameter.Frequency = this.ntbFrequency.Value;
				parameter.NumberOfPeriods = (double)this.nudColorPeriods.Value;
				parameter.Translation = this.tbTranslation.Value / 100.0;
				parameter.Coloring = (RenderParameterEscapeTimeSmoother.Colorings)this.cbColoring.SelectedEnum;
				parameter.UserColor1 = this.cbUserColor1.SelectedColor;
				parameter.UserColor2 = this.cbUserColor2.SelectedColor;
				parameter.BackgroundColor = this.cbBackground.SelectedColor;
				return parameter;
			}
			set
			{
				this.requestRedraw = false;
				this.rbNumberOfPeriods.Checked = value.AutoSetFrequency;
				this.ntbFrequency.Text = value.Frequency.ToString();
				this.nudColorPeriods.Value = (decimal)value.NumberOfPeriods;
				this.tbTranslation.Value = (int)(value.Translation * 100);
				this.cbColoring.Select(value.Coloring.ToUInt64());
				this.cbUserColor1.SelectColor(value.UserColor1);
				this.cbUserColor2.SelectColor(value.UserColor2);
				this.cbBackground.SelectColor(value.BackgroundColor);
				this.requestRedraw = true;
			}
		}

		ArgusLib.Fractals.IRenderParameter IRenderParameterInputControl.Parameter
		{
			get { return this.Parameter; }
			set { this.Parameter = (RenderParameterEscapeTimeSmoother)value; }
		}

		public Control Control { get { return this; } }
		#endregion

		#region Non-Public Methods
		private void cb_CheckedChanged(object sender, EventArgs e)
		{
			this.nudColorPeriods.Enabled = this.rbNumberOfPeriods.Checked;
			this.ntbFrequency.Enabled = this.rbFrequency.Checked;
			this.OnRedrawRequested(this, EventArgs.Empty);
		}

		private void cbColoring_SelectedIndexChanged(object sender, EventArgs e)
		{
			RenderParameterEscapeTimeSmoother.Colorings coloring = (RenderParameterEscapeTimeSmoother.Colorings)this.cbColoring.SelectedEnum;
			if (coloring == RenderParameterEscapeTimeSmoother.Colorings.UserDefined1 || coloring == RenderParameterEscapeTimeSmoother.Colorings.UserDefined2)
			{
				this.cbUserColor1.Enabled = true;
				this.cbUserColor2.Enabled = true;
			}
			else
			{
				this.cbUserColor1.Enabled = false;
				this.cbUserColor2.Enabled = false;
			}
			this.OnRedrawRequested(this, EventArgs.Empty);
		}
		#endregion

		#region Eventhandling
		public event EventHandler RedrawRequested;

		private void OnRedrawRequested(object sender, EventArgs e)
		{
			if (this.requestRedraw == false)
				return;
			if (this.RedrawRequested != null)
				this.RedrawRequested(this, EventArgs.Empty);
		}
		#endregion
	}
}
